package com.report.utils;

import com.freewayso.image.combiner.ImageCombiner;

import com.freewayso.image.combiner.enums.OutputFormat;
import com.freewayso.image.combiner.enums.ZoomMode;
import com.zmh.framework.exception.ResultException;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;


import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/13 12:50
 */
public class ImageUtils {


    public static void main(String[] args) {
        sendPoster("");
    }

    public static void sendPoster(String name)  {
        try {
            String bgImageUrl = "https://img.thebeastshop.com/combine_image/funny_topic/resource/bg_3x4.png";
            BufferedImage avatar = ImageIO.read(new URL("https://oss.xinyanbao.cn/image/headPortrait.png"));
            ImageCombiner combiner = new ImageCombiner("https://yw.xinyanbao.cn/image/WechatIMG1473.jpg", OutputFormat.JPG);
            combiner.addImageElement("https://yw.xinyanbao.cn/2024-08-24/PR5TX9T9KN5k85c28e3785ebdfc44cce2cf5046b3745.jpeg", 120, 160, 160, 160, ZoomMode.WidthHeight)

                    .setRoundCorner(200);
            combiner.addTextElement("腾讯新职业" , 40, 300, 200)
                    .setAutoBreakLine(837, 2, 60);
            combiner.addImageElement("https://oss.xinyanbao.cn/2024-07-25/Vc1p-gvNb_rVg-T0he8cC.PNG", 100, 380, 300, 400, ZoomMode.WidthHeight)
                    .setRoundCorner(46)
                    .setCenter(true);


             combiner.addTextElement("新研报查一查", 80, 100, 980)
                     .setColor(101,140,215);
            combiner.addImageElement("https://oss.xinyanbao.cn/2024-07-25/Vc1p-gvNb_rVg-T0he8cC.PNG",600,1000,300,300,ZoomMode.WidthHeight)
                    .setRoundCorner(50);

            combiner.addTextElement("专业全面的个人智库" , 40, 100, 1100);
            combiner.addTextElement("长按识别，免费查看报告" , 40, 100, 1300)
                    .setColor(64,70,65);
            //标题（默认字体为“阿里巴巴普惠体”，也可以自己指定字体名称或Font对象）https://yw.xinyanbao.cn/2024-08-25/b8f7b84c-c904-4ca2-95fd-02cbe15f60163607952354271245546.jpg
            //内容（设置文本自动换行，需要指定最大宽度（超出则换行）、最大行数（超出则丢弃）、行高）

            //combiner.addImageElement(code, 90, 1690, 220, 220, ZoomMode.WidthHeight);
            combiner.combine();
            combiner.save("/Users/Zhuanz/Desktop/pdf/a.jpg");

        }catch (Exception e){
           throw new ResultException(e.getMessage());
        }

     }

    public static InputStream sendPosterInput(String path,String code,String name,String avatarUrl)  {
        try {
            String bgImageUrl = "https://yw.xinyanbao.cn/image/WechatIMG1473.jpg";
            BufferedImage avatar = ImageIO.read(new URL(avatarUrl));
             ImageCombiner combiner = new ImageCombiner(bgImageUrl, OutputFormat.PNG);
            //combiner.setBackgroundBlur(30);     //设置背景高斯模糊（毛玻璃效果）
            combiner.setCanvasRoundCorner(30);


            combiner.addImageElement(avatar, 120, 160, 160, 160, ZoomMode.WidthHeight)

                    .setRoundCorner(200);

            combiner.addImageElement(path, 100, 380, 430, 580, ZoomMode.WidthHeight)
                    .setRoundCorner(46)
                    .setCenter(true);
            combiner.addTextElement(name ,"/fonts/simsun.ttc", Font.BOLD, 60, 150, 1240)
                    .setAutoBreakLine(880, 2, 60);
            combiner.addImageElement(code,730,1600,300,300,ZoomMode.WidthHeight);


            combiner.combine();
            return combiner.getCombinedImageStream();
        }catch (Exception e){
            throw new ResultException(e.getMessage());
        }

    }

    public static String BufferedImageToBase64(BufferedImage bufferedImage) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
        try {
            ImageIO.write(bufferedImage, "png", baos);//写入流中
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] bytes = baos.toByteArray();//转换成字节
        BASE64Encoder encoder = new BASE64Encoder();
        String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串
        png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
        return "data:image/png;base64," + png_base64;
    }

}
